package com.biejiaogroup.librarymanager.service;

import com.biejiaogroup.librarymanager.entity.Book;
import com.biejiaogroup.librarymanager.entity.User;
import com.biejiaogroup.librarymanager.entity.vo.MessageModel;
import com.biejiaogroup.librarymanager.mapper.BookMapper;
import com.biejiaogroup.librarymanager.mapper.UserMapper;
import com.biejiaogroup.librarymanager.util.GetSQLSession;
import com.biejiaogroup.librarymanager.util.StringUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Vector;

/**
 * @Description: 用户服务类
 * @Author: JesseChan
 * @Date: 2022/5/6
 */
public class UserService {
    /**
     * @param userName
     * @param userPassword
     * @return: com.biejiaogroup.librarymanager.entity.vo.MessageModel
     * @Description: 用户登录
     * @Author: JesseChan
     * @Date: 2022/5/6
     */
    public MessageModel adminLogin(String userName, String userPassword)
    {
        MessageModel messageModel = new MessageModel();

        User u = new User();
        u.setUserName(userName);
        u.setUserPassword(userPassword);
        messageModel.setObject(u);

        if(StringUtil.isEmpty(userName) || StringUtil.isEmpty(userPassword))
        {
            messageModel.setCode(0);
            messageModel.setMsg("用户名和密码不能为空");
            return messageModel;
        }

        //2.获取SqlSession对象
        SqlSession session = GetSQLSession.createSQLSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        u = userMapper.getUserByName(userName);

        //3.判断用户是否存在
        if(u == null)
        {
            messageModel.setCode(0);
            messageModel.setMsg("用户不存在");
            return messageModel;
        }

        //4.比较密码
        if(!userPassword.equals(u.getUserPassword()))
        {
            messageModel.setCode(0);
            messageModel.setMsg("用户密码错误");
            return messageModel;
        }

        //5.是否管理员 用户权限等级 0管理员 1普通用户
        if(u.getUserLevel() != 0)
        {
            messageModel.setCode(0);
            messageModel.setMsg("该用户不是管理员");
            return messageModel;
        }

        messageModel.setObject(u);
        return messageModel;
    }

    /**
     * @param
     * @return: java.util.Vector<com.biejiaogroup.librarymanager.entity.User>
     * @Description: 获取所有用户
     * @Author: JesseChan
     * @Date: 2022/5/13
     */
    public Vector<User> getAll()
    {
        Vector<User> v;
        SqlSession session = GetSQLSession.createSQLSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        v = userMapper.getAllUser();
        session.close();
        return v; //返回所有用户
    }

    /**
     * @param userName
     * @return: java.util.Vector<com.biejiaogroup.librarymanager.entity.User>
     * @Description: 根据用户名模糊搜索
     * @Author: JesseChan
     * @Date: 2022/5/13
     */
    public Vector<User> getAllUserByName(String userName)
    {
        Vector<User> v;
        SqlSession session = GetSQLSession.createSQLSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        v = userMapper.getAllUserByName(userName);
        session.close();
        return v;
    }


    /**
     * @param userName
     * @param userPassword
     * @return: com.biejiaogroup.librarymanager.entity.vo.MessageModel
     * @Description: 用户登录
     * @Author: JesseChan
     * @Date: 2022/5/14
     */
    public MessageModel userLogin(String userName, String userPassword)
    {
        MessageModel messageModel = new MessageModel();

        User u = new User();
        u.setUserName(userName);
        u.setUserPassword(userPassword);
        messageModel.setObject(u);

        if(StringUtil.isEmpty(userName) || StringUtil.isEmpty(userPassword))
        {
            messageModel.setCode(0);
            messageModel.setMsg("用户名和密码不能为空");
            return messageModel;
        }

        //2.获取SqlSession对象
        SqlSession session = GetSQLSession.createSQLSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        u = userMapper.getUserByName(userName);

        //3.判断用户是否存在
        if(u == null)
        {
            messageModel.setCode(0);
            messageModel.setMsg("用户不存在");
            return messageModel;
        }

        //4.比较密码
        if(!userPassword.equals(u.getUserPassword()))
        {
            messageModel.setCode(0);
            messageModel.setMsg("用户密码错误");
            return messageModel;
        }

        messageModel.setObject(u);
        return messageModel;
    }

    /**
     * @param userName
     * @return: boolean
     * @Description: 检查用户名是否已被注册
     * @Author: JesseChan
     * @Date: 2022/5/16
     */
    public boolean isUserNameRegisted(String userName)
    {
        User user = new User();
        SqlSession session = GetSQLSession.createSQLSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        user = userMapper.getUserByName(userName);
        session.close();
        if(user == null) return false;
        else return true;
    }

    /**
     * @param user
     * @return: void
     * @Description: 注册用户
     * @Author: JesseChan
     * @Date: 2022/5/16
     */
    public void register(User user)
    {
        SqlSession session = GetSQLSession.createSQLSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        userMapper.register(user);
        session.commit();
        session.close();
    }
}
